/* GUI CONVERTED from reoptr1b.p (converter v1.75) Sat Aug 12 23:07:29 2000 */
/* reoptr1b.p - LOCATE REPETITIVE SCHEDULE TO UPDATE                    */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                  */
/* All rights reserved worldwide.  This is an unpublished work.         */
/*F0PN*/ /*V8:ConvertMode=Maintenance                                   */
/*K1Q4*/ /*V8:WebEnabled=No                                             */
/* REVISION: 7.3      LAST MODIFIED: 01/21/93   BY: emb *G689*          */
/* REVISION: 7.3      LAST MODIFIED: 02/15/95   BY: ais *G0FC*          */
/* REVISION: 7.3      LAST MODIFIED: 05/11/95   BY: ais *G0MM*          */
/* REVISION: 7.3      LAST MODIFIED: 03/25/96   BY: jym *G1RD*          */
/* REVISION: 7.3    LAST MODIFIED: 07/15/96 BY: *G1Z7* Julie Milligan   */
/* REVISION: 7.4    LAST MODIFIED: 10/24/97 BY: *H1G2* Sandesh Mahagaokar */
/* REVISION: 8.6    LAST MODIFIED: 05/20/98 BY: *K1Q4* Alfred Tan         */
/* REVISION: 9.1    LAST MODIFIED: 08/12/00 BY: *N0KP* myb                */

         {mfdeclre.i}

         define input parameter site as character.
         define input parameter production_line as character.
         define input parameter part as character.
         define input parameter operation as integer.
         define input parameter effdate as date.
         define output parameter route-code as character.
         define output parameter bom-code as character.
         define output parameter id as character.

         define variable cons_bck like rpc_sch_bck no-undo.
         define variable cons_fwd like rpc_sch_fwd no-undo.
         define variable wk_start like rpc_wk_start no-undo.
         define variable rps_start like rps_due_date no-undo.
/*G0MM*/ define variable unexploded_schedules as logical.
/*G0MM*/ define variable schedule_type as integer.

/*G0MM*/ find mfc_ctrl where mfc_field = "rpc_using_new" no-lock no-error.
/*G1Z7* /*G0MM*/ if available mfc_ctrl and mfc_logical = true then leave. */
/*G1Z7* * * BEGIN ADD SECTION * * VALIDATION FOR ADV. REPETITIVE          */
        /* IF id = ?, THEN SCHEDULE NEEDS TO BE EXPLODED                  */
/*G1Z7*/ if available mfc_ctrl and mfc_logical = true then do:
           id = "".
           for each rps_mstr no-lock where
             rps_qty_comp < rps_qty_req and
             rps_part = part and
             rps_site = site and
             rps_line = production_line:
             find wo_mstr no-lock where wo_part = rps_part and
               wo_site = rps_site and
               wo_line = rps_line and
               wo_type = "S" and
               wo_due_date = rps_due_date and
               wo_qty_ord = rps_qty_req
               no-error.
             if not available wo_mstr then do:
               id = "?".
               leave.
             end.
           end. /* FOR EACH rps_mstr */
         end. /* ADVANCED REPETITIVE*/
         else do:
/*G1Z7* * *END ADD SECTION */

/*G0MM*/   unexploded_schedules = no.
/*G0MM*/   schedule_type = 0.
           rps_start = effdate.

           find first rpc_ctrl no-lock no-error.
           if available rpc_ctrl then
           assign cons_bck = rpc_sch_bck
                cons_fwd = rpc_sch_fwd
                wk_start = rpc_wk_start.

           if wk_start < 8 then do:
             if wk_start = 0 then rps_start = rps_start - cons_bck.
             else do:
               if weekday(rps_start) >= wk_start
               then rps_start = rps_start
                  - weekday(rps_start) + wk_start.
               else rps_start = rps_start
                  - weekday(rps_start) + wk_start - 7.
             end.
           end.

           find ptp_det no-lock where ptp_part = part
           and ptp_site = site no-error.
           if available ptp_det then do:
              assign route-code = ptp_routing
                     bom-code = ptp_bom_code.
           end.
           else do:
             find pt_mstr no-lock where pt_part = part no-error.
             if available pt_mstr then
              assign route-code = pt_routing
                     bom-code = pt_bom_code.
           end.

           default-sched:
           do:
/*G0FC*
      *     CODE REMOVED - WE WANT TO START OUR USE-UP LOGIC ON THE FIRST
      *     OPERATION MEETING THE DATE CRITERIA, NOT THE FIRST ONE WITH
      *     A REMAINING QUANTITY.  SEE PATCH G0FC FOR REASONING.
      *     /* 1st - look for exploded schedules with uncompleted operations
      *              scheduled to be completed on effective date */
      *     for each rps_mstr no-lock use-index rps_part
      *     where rps_part = part
      *     and rps_site = site
      *     and rps_line = production_line
      *     and rps_due_date >= rps_start
      *     and rps_due_date <= effdate + cons_fwd,
      *     first wr_route no-lock use-index wr_lot
      *     where wr_lot = string(rps_record)
      *     and wr_op = operation
      *     and wr_part = part
      *     and wr_due = effdate
      *     and wr_qty_ord > wr_qty_comp
      *     by wr_start:
      *
      *        route-code = rps_routing.
      *        bom-code = rps_bom_code.
      *        id = string(rps_record).
      *
      *        leave default-sched.
      *     end.
      *
      *     /* 2nd - look for exploded schedules with uncompleted operations
      *              scheduled to be start on or before effective date */
      *     for each rps_mstr no-lock use-index rps_part
      *     where rps_part = part
      *     and rps_site = site
      *     and rps_line = production_line
      *     and rps_due_date >= rps_start
      *     and rps_due_date <= effdate + cons_fwd,
      *     first wr_route no-lock use-index wr_lot
      *     where wr_lot = string(rps_record)
      *     and wr_op = operation
      *     and wr_part = part
      *     and wr_start <= effdate
      *     and wr_qty_ord > wr_qty_comp
      *     by wr_start:
      *
      *        route-code = rps_routing.
      *        bom-code = rps_bom_code.
      *        id = string(rps_record).
      *
      *        leave default-sched.
      *     end.
      *
      *     /* 3rd - look for exploded schedules with uncompleted operations */
      *     for each rps_mstr no-lock use-index rps_part
      *     where rps_part = part
      *     and rps_site = site
      *     and rps_line = production_line
      *     and rps_due_date >= rps_start
      *     and rps_due_date <= effdate + cons_fwd,
      *     first wr_route no-lock use-index wr_lot
      *     where wr_lot = string(rps_record)
      *     and wr_op = operation
      *     and wr_part = part
      *     and wr_qty_ord > wr_qty_comp
      *     by wr_start:
      *
      *        route-code = rps_routing.
      *        bom-code = rps_bom_code.
      *        id = string(rps_record).
      *
      *        leave default-sched.
      *     end.
      *
**G0FC*/    /* END OF DELETED CODE */

              /* 4th - look for exploded schedules which were supposed
                       to be completed on effective date (no longer open) */
              for each rps_mstr no-lock use-index rps_part
              where rps_part = part
              and rps_site = site
              and rps_line = production_line
              and rps_due_date >= rps_start,
/*G0FC        and rps_due_date <= effdate + cons_fwd,     */
              first wr_route no-lock use-index wr_lot
              where wr_lot = string(rps_record)
              and wr_op = operation
              and wr_part = part
/*G0FC*/      and wr_start <= effdate
/*G0FC*/      and wr_due >= effdate

/*G0FC        and wr_due = effdate                        */
              by wr_start:

                 route-code = rps_routing.
                 bom-code = rps_bom_code.
                 id = string(rps_record).

/*G0MM*/         schedule_type = 4.
                 leave default-sched.
              end.

            /* 5th - look for exploded schedules which were supposed to be
                     started on or before effective date (no longer open) */
              for each rps_mstr no-lock use-index rps_part
              where rps_part = part
              and rps_site = site
              and rps_line = production_line
              and rps_due_date >= rps_start,
/*G0FC        and rps_due_date <= effdate + cons_fwd,   */
              first wr_route no-lock use-index wr_lot
              where wr_lot = string(rps_record)
              and wr_op = operation
              and wr_part = part
              and wr_start <= effdate
              by wr_start:

                route-code = rps_routing.
                bom-code = rps_bom_code.
                id = string(rps_record).

/*G0MM*/        schedule_type = 5.
                leave default-sched.
              end.

            /* 6th - look for exploded schedules (no longer open) */
              for each rps_mstr no-lock use-index rps_part
              where rps_part = part
              and rps_site = site
              and rps_line = production_line
              and rps_due_date >= rps_start
              and rps_due_date <= effdate + cons_fwd,
              first wr_route no-lock use-index wr_lot
              where wr_lot = string(rps_record)
              and wr_op = operation
              and wr_part = part
              by wr_start:

                 route-code = rps_routing.
                 bom-code = rps_bom_code.
                 id = string(rps_record).

/*G0MM*/         schedule_type = 6.
                 leave default-sched.
              end.

/*G1RD* * * DELETED SECTION*
.            /* 7th - Look for unexploded open schedules in the date range */
.            for each rps_mstr no-lock where rps_part = part
.            and rps_site = site
.            and rps_line = production_line
.            and rps_due_date >= rps_start
.            and rps_due_date <= effdate + cons_fwd
.            and rps_qty_req > rps_qty_comp:
.               route-code = rps_routing.
.               bom-code = rps_bom_code.
.               id = string(rps_record).
.
./*G0MM*/       schedule_type = 7.
.               leave default-sched.
.            end.
.
.            /* 8th - Look for unexploded schedules in the date range */
.            for each rps_mstr no-lock where rps_part = part
.            and rps_site = site
.            and rps_line = production_line
.            and rps_due_date >= rps_start
.            and rps_due_date <= effdate + cons_fwd:
.
.               route-code = rps_routing.
.               bom-code = rps_bom_code.
.               id = string(rps_record).
.
./*G0MM*/       schedule_type = 8.
.               leave default-sched.
.            end.
.
**G1RD* * * * END DELETED SECTION*/

           end.

/*G0MM*/ /* ADDED NEW SECTION.  ID WILL BE SET TO ? IF THERE ARE BOTH SCHEDULES
            AND UNEXPLODED SCHEDULES WITHIN THE CONSUMPTION PERIOD           */
/*G1RD* * *DELETED SECTION * * *
./*G0MM*/ if schedule_type = 7 or schedule_type = 8
.         then unexploded_schedules = true.          /* NO EXPLODED SCHEDULES */
.         else if schedule_type = 0
.         then next.                                 /* NO SCHEDULES - NO PROB*/
.         else do:
**G1RD* * *END DELETED SECTION * * */

            /* MAKE SURE THAT ALL SCHEDULES WITHIN CONSUMPTION PERIOD
               HAVE BEEN EXPLODED  */
              for each rps_mstr no-lock use-index rps_part
              where rps_part = part
              and rps_site = site
              and rps_line = production_line
              and rps_due_date >= rps_start
              and rps_due_date <= effdate + cons_fwd:

/* WHEN THE SCHEDULE INDICATES THE NEED TO PLAN TO MAKE MORE OF AN ITEM,   */
/* THEN AFTER THE EXPLOSION A WR_ROUTE RECORD WILL EXISTS IN WHICH THE     */
/* WR_QTR_ORD WILL EQUAL THE RPS_QTY_REQ.  IF THE REQUIREMENT HAS BEEN     */
/* FULLY COMPLETED, THEN THE WR_ROUTE RECORD WILL BE REMOVED.              */

/*H1G2*  REPORTING SCRAP WILL UPDATE wr_qty_ord. HENCE rps_qty_req SHOULD  */
/*       BE CHECKED ONLY WITH wo_qty_ord.                                  */

                 find first wr_route no-lock use-index wr_lot
                    where wr_lot = string(rps_record)
                    and wr_op = operation
                    and wr_part = part
/*H1G2** /*G1RD*/   and wr_qty_ord = rps_qty_req */
                 no-error.
                 if not available wr_route
/*G1RD*/           and rps_qty_req > rps_qty_comp
                 then do:
                    unexploded_schedules = true.
                    leave.
                 end.

/*H1G2*  BEGIN ADD SECTION */
                 else if available wr_route
                 then do:
                    if not can-find(wo_mstr where wo_lot = wr_lot
                                    and wo_qty_ord = rps_qty_req)
                    then do:
                       unexploded_schedules = true.
                       leave.
                    end.
                 end. /* ELSE IF AVAILABLE wr_route */
/*H1G2*  END ADD SECTION */

              end. /* for each rps_mstr */
/*G1RD*           end. */
           if unexploded_schedules
/*G0MM*/   then id = ?.
/*G1Z7*/ end. /* NOT ADV. REPETITIVE */
